package com.useeinfo.oa.modules.inspection.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.inspection.entity.PlannerInspection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("PlannerInspectionDao")
public class PlannerInspectionDao implements CrudDao<PlannerInspection> {
    private final static Logger logger = LoggerFactory.getLogger(PlannerInspectionDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("p.deleteStatus = ?{paramIndex} ", false);

        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        for (String key : queryHash.keySet()) {
            String value = queryHash.get(key);
            switch (key) {
                case "applicationName":
                    conditionHash.put("p.applicationName like ?{paramIndex}", "%" + value + "%");
                    continue;
                case "enterpriseId":
                    conditionHash.put("p.enterprise.id = ?{paramIndex}",StringConverters.null2Long(value));
                    continue;
                case "searchCode":
                    conditionHash.put(" (p.name like ?{paramIndex} or p.serial like ?{paramIndex} or p.required like ?{paramIndex} or " +
                            "p.degree like ?{paramIndex} or p.type like ?{paramIndex} or p.organization.organizationName like ?{paramIndex} )","%"+value+"%");
                    continue;
                case "name":
                    conditionHash.put("p.name like ?{paramIndex}","%"+value+"%");
                    continue;
                case "required":
                    conditionHash.put("p.required like ?{paramIndex}",value);
                    continue;
                case "start":
                    conditionHash.put("p.startTime >= ?{paramIndex}",StringConverters.ToDateOnly(value));
                    continue;
                case "end":
                    conditionHash.put("p.endTime <= ?{paramIndex}",StringConverters.ToDateOnly(value));
                    continue;
                case "period":
                    conditionHash.put("p.periodInstall.name like ?{paramIndex}",value);
                    continue;
                case "orderName":
                    conditionHash.put("p.name = ?{paramIndex}",value);
                    continue;

            }
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(p) from PlannerInspection p ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<PlannerInspection> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select p from PlannerInspection p ", "order by p.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<PlannerInspection> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, PlannerInspection.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public PlannerInspection findModel(Long id) {
        return entityManager.find(PlannerInspection.class, id);
    }

    @Override
    public Integer add(PlannerInspection model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("PlannerInspectionDaoImpl添加PlannerInspection成功！id为："+model.getId());
        return 1;
    }

    @Override
    public Integer update(PlannerInspection model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        logger.info("更新成功！id为："+model.getId());
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        PlannerInspection plannerInspection = entityManager.find(PlannerInspection.class, id);
        entityManager.remove(plannerInspection);
        return 1;
    }

    @Override
    public Integer deleteLogic(PlannerInspection model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
